package com.pyb.java.commonutil.eventbus;

import java.io.Serializable;
import java.util.HashMap;

/**
 * EventBus 消息 传递数据主题
 */
public class EventBusMessage implements Serializable {
    /**
     * 消息的key
     */
    private int what;

    /**
     * 消息的主体message
     */
    private Object message;

    private HashMap messageMap;

    public int getKey() {
        return what;
    }

    public void setKey(int what) {
        this.what = what;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public EventBusMessage(int what, Object message) {
        this.what = what;
        this.message = message;
    }

    public EventBusMessage(int what) {
        this.what = what;
    }

    EventBusMessage put(String key, Object message) {
        if(messageMap == null) {
            messageMap = new HashMap();
        }
        messageMap.put(key, message);
        return this;
    }

    public <T> T get(String key) {
        if(messageMap != null) {
            try{
                return (T) messageMap.get(key);
            }catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
